<?php
/**
 * Application level Controller
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * PHP versions 4 and 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       cake
 * @subpackage    cake.cake.libs.controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

/**
 * This is a placeholder class.
 * Create the same file in app/app_controller.php
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package       cake
 * @subpackage    cake.cake.libs.controller
 * @link http://book.cakephp.org/view/957/The-App-Controller
 */
class AppController extends Controller {
	var $components = array('Auth','Session','Dependence'); //,'Acl');
	
	function beforeFilter() {
		$this->set('controller_start', microtime(true));
		
		if (!defined('CRON_DISPATCHER')) {
			$this->Auth->allow('index');
			$this->Auth->loginError = "Incorrect username or password";
			$this->Auth->authError = "Please login to access this area";
			$this->Auth->loginRedirect = array('controller'=>'feeds','action'=>'view');
			$this->Auth->logoutRedirect = array('controller'=>'users','action'=>'login');
			$this->set('admin', $this->_isAdmin());
			$this->set('logged_in', $this->_loggedIn());
			$this->set('logged_in_user', $this->_username());
			$this->set('logged_in_id', $this->Auth->user('id'));
			$this->Dep = new DependenceComponent();
			$this->set('dep', new DependenceComponent());
			
			if ($this->_loggedIn()) {
				$this->_friendsList();
				$this->set('user_obj', new User());
				$folder = new Feedfolder();
				$folders = $folder->find('all', array('conditions' => array('user_id'=>$this->Auth->user('id'),'root'=>0), 'recursive'=>0));
				$feeds = $folder->Feed->find('all', array('recursive'=>0));
				$this->set('feed_folders', $folders);
			}
		}
	}

  function _friendsList() {
		$user = new User();
		
		$friends = $user->find('all', array('recursive'=>0));
		
		$this->set('friends', $friends);
	} // _friendsList()

	function _isAdmin() {
		$admin = false;
		if ($this->Auth->user('roles') == 'admin') {
			$admin = true;
		}
		
		return $admin;
	} // _isAdmin()
	
	function _loggedIn() {
		$logged_in = false;
		if ($this->Auth->user()) {
			$logged_in = true;
		}
		return $logged_in;
	} // _loggedIn()
	
	function _username() {
		$username = null;
		if ($this->Auth->user()) {
			$username =  $this->Auth->user('username');
		}
		return $username;
	} // _username()
}
